﻿using System.Web.Mvc;

namespace ProgrammingSchool.Infrastructure.Filters.Authorize
{
    public class GlobalAuthorizeAttribute : AuthorizeAttribute
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (!SkipAuthorization(filterContext))
            {
                base.OnAuthorization(filterContext);
            }
        }

        private static bool SkipAuthorization(AuthorizationContext filterContext)
        {
            return filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) ||
                                     filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true);
        }
    }
}